﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Employees_EmpWorkExprienceList : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["action"] != null)
            {
                InitializeComponent(true);


                WMaster.SetWhere("Employees.EmployeeID=" + Request["eid"].ToString());
                Master.DataSource = WMaster;
            }
            else
            {
                InitializeComponent(false);
                Master.DataSource = WMaster;

                wgvMaster.Visible = false;
            }

            ((HiddenField)this.Page.Master.FindControl("HoverId")).Value = "EmpWorkExprience";




            MasterPage_MasterPage_EmpDetail master = (MasterPage_MasterPage_EmpDetail)this.Page.Master;

            master.HelpHref = "";
            master.PageName = "员工工作经历";
            master.PicUrl = "~/image/PageIcon/jiangli.jpg";
            master.IntiBar();
        }
    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Employees_EmpWorkExprienceList));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = action;
        this.WMaster.AlwaysClose = false;
        this.WMaster.PacketRecords = -1;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.EmpWorkExprience";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;


            ImageButton imb = (ImageButton)e.Row.FindControl("imb");

            string action = "";
            if (Request["action"] != null)
            {
                action = "&action=" + Request["action"].ToString();
            }

            string mode = "";
            if (Request["mode"] != null)
            {
                mode = "&mode=" + Request["mode"].ToString();
            }

            if (drv["Eid"].ToString() != "0" && drv["Eid"].ToString() != "")
            {
                imb.CommandArgument = "Eid=" + drv["EmployeeId"].ToString() + "&ename=" + Server.UrlEncode(drv["EmpName"].ToString()) + "&Wid=" + drv["Eid"].ToString() + action + mode;
            }

        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        string mode = "";
        if (Request["mode"] != null)
        {
            mode = "&mode=" + Request["mode"].ToString();
        }
        Response.Redirect(Server.HtmlDecode(Request["redurl"].ToString()) + mode);
    }


    protected void wgvMaster_DataBound(object sender, EventArgs e)
    {

        if (Request["action"] != null)
        {
            SearchTb.Visible = false;

            if (Request["action"].ToString() == "edit")
            {

                TabsFunction.Visible = true;

                string eid = Request["eid"].ToString();
                object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpNameByEid", new object[] { eid });
                string ename = oj[1].ToString();

                AddFunction.Attributes.Add("onclick", "ymPrompt.win({title:'工 作 经 历 添 加',message:'EmpWorkExprience.aspx?eid=" + eid + "&ename=" + Server.UrlEncode(ename)
                + "&action=onlyadd',closeBtn:true,iframe:true,allowRightMenu:false,allowSelect:false});ymPrompt.max();return false");
            }
            else
            {
                TabsFunction.Visible = false;
            }
        }
        else
        {
            TabsFunction.Visible = false;
        }

    }

    protected void wgvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "show")
        {
            if (e.CommandArgument.ToString() != "")
            {
                Response.Redirect("EmpWorkExprience.aspx?" + e.CommandArgument.ToString());
            }
        }
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        string strDep = Depart.SelectDepmanetId;
        if (strDep == "")
        {
            return;
        }
        else if (strDep == "0")
        {
            strDep = "";
        }
        else
        {
            strDep = " And Employees.DepartmentId like ('" + strDep + "%')";
        }


        string sname = SelUser.EmpName;
        if (sname != "")
        {
            sname = " And Employees.EmpName like ('%" + sname + "%')";
        }
        else
        {
            sname = "";
        }

        InitializeComponent(true);

        WMaster.SetWhere("1=1 " + strDep + sname);
        Master.DataSource = WMaster;
        wgvMaster.Visible = true;
    }

    protected void Add_Click(object sender, EventArgs e)
    {
        Response.Redirect("EmpWorkExprience.aspx?ItemParam=");
    }

    protected void DownLoad_Click(object sender, EventArgs e)
    {
        try
        {
            object obj = Master.InnerDataSet;

            string path = MapPath("~/UploadFile/Template/");

            DataTable dt = ((DataSet)obj).Tables[0];


            if (dt.Rows.Count <= 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('没有记录')", true);
                return;
            }
            DataTable dts = GetDataTable();

            foreach (DataRow dr in dt.Rows)
            {
                DataRow SR = dts.NewRow();
                SR["编号"] = dr["Eid"];
                SR["员工编号"] = dr["EmployeeID"];
                SR["员工姓名"] = dr["EmpName"];
                SR["开始年月"] = Convert.ToDateTime(dr["StartDate"]).ToString("yyyy-MM");
                SR["结束年月"] = Convert.ToDateTime(dr["EndDate"]).ToString("yyyy-MM");
                SR["就职单位"] = dr["Company"];
                SR["职位名称"] = dr["JobName"];
                SR["职位描述"] = dr["WorkDesc"];
               
                dts.Rows.Add(SR);
            }


            DataTable dtt = dts.Clone();
            dtt.Merge(dts);
            Srvtools.InfoDataSet ids = new Srvtools.InfoDataSet();
            ids.RemoteName = "GLModule.cmdColDEF";
            ids.RealDataSet.Tables.Add(dtt);

            ids.ToExcel(0, path + "empwkexp.xls");

            System.IO.FileInfo fn = new System.IO.FileInfo(path + "empwkexp.xls");

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
            HttpContext.Current.Response.WriteFile(fn.FullName);
            HttpContext.Current.Response.End();
        }
        catch
        {

        }
    }

    private DataTable GetDataTable()
    {
        DataTable WTable = new DataTable("DownLoadDT");
        {
            WTable.Columns.Add(new DataColumn("编号", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("员工编号", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("员工姓名", typeof(string)));
            WTable.Columns.Add(new DataColumn("开始年月", typeof(string)));
            WTable.Columns.Add(new DataColumn("结束年月", typeof(string)));
            WTable.Columns.Add(new DataColumn("就职单位", typeof(string)));
            WTable.Columns.Add(new DataColumn("职位名称", typeof(string)));
            WTable.Columns.Add(new DataColumn("职位描述", typeof(string)));
        }
        return WTable;
    }
}
